package joodle.ui;
/**
 * Represents the "Save" action on menu bar
 */
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.io.File;
import java.io.IOException;

import javax.swing.AbstractAction;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.KeyStroke;
import javax.swing.filechooser.FileNameExtensionFilter;

import joodle.si.IOManager;
import joodle.si.ResourceManager;

@SuppressWarnings("serial")
public class SVGSaveAction extends AbstractAction {

	public SVGSaveAction() {
		super(ResourceManager.message("SAVE"), ResourceManager.makeIcon("save"));
		putValue(MNEMONIC_KEY, KeyEvent.VK_S);
		putValue(ACCELERATOR_KEY, KeyStroke.getKeyStroke("control S"));
	}
	/**
	 * @see AbstractAction#actionPerformed(ActionEvent)
	 */
	@Override
	public void actionPerformed(ActionEvent ae) {
		JFileChooser fc = new JFileChooser();
		FileNameExtensionFilter ff = new FileNameExtensionFilter("Scalable Vector Graphics (SVG)", "svg");
		fc.setFileFilter(ff);
		if (fc.showSaveDialog(null) == JFileChooser.APPROVE_OPTION) {
			File f = fc.getSelectedFile();
			if(!f.getName().toLowerCase().endsWith(".svg"))
				f = new File(f.getAbsolutePath() + ".svg");
			try {
				IOManager.saveXML(f);
			} catch (IOException e) {
				JOptionPane.showMessageDialog(fc,
						"Joodle encountered an error while saving!", "Error",
						JOptionPane.ERROR_MESSAGE);
			}
		}
	}

}
